package top.minuy.subject.leetcode._303;
/**
 * @author Minuy
 * @time 19:18
 * @date 2021/10/4
 */
public class NumArray {
    SegmentTree segmentTree;
    public NumArray(int[] nums) {
        segmentTree = new SegmentTree(nums,(a,b)-> a+b);
    }

    public int sumRange(int left, int right) {
        return segmentTree.query(left,right);
    }

    /**
     * 合并接口，将两个元素合并为一个
     */
    interface Merger {
        /**
         * 合并两个元素
         * @param a 元素a
         * @param b 元素b
         * @return 合并后的元素
         */
        int merge(int a, int b);
    }

    class SegmentTree {
        private int[] data;
        private int[] tree;
        Merger merger;

        public SegmentTree(int[] arr, Merger merger) {
            this.merger = merger;

            data =  new int[arr.length];
            tree =  new int[arr.length * 4];

            for (int i = 0; i < arr.length; i++) {
                data[i] = arr[i];
            }

            buildSegmentTree(0, 0, arr.length - 1);
        }

        private void buildSegmentTree(int treeIndex, int l, int r) {
            // 要构建的线段树只有一个元素了
            if (l == r) {
                // 直接赋值即可
                tree[treeIndex] = data[l];
                return;
            }

            // 计算分割位置和计算左右节点位置
            int mid = l + (r - l) / 2;
            int leftChildIndex = getLeftChild(treeIndex);
            int rightChildIndex = getRightChild(treeIndex);

            // 构建左子树
            buildSegmentTree(leftChildIndex, l, mid);
            // 构建右子树
            buildSegmentTree(rightChildIndex, mid + 1, r);

            // 合并两个子节点
            tree[treeIndex] = merger.merge(tree[leftChildIndex], tree[rightChildIndex]);
        }

        public int getSize() {
            return data.length;
        }

        public int get(int index) {
            if (index < 0 || index >= data.length) {
                throw new IllegalArgumentException("Get failed. Index is illegal.");
            }
            return data[index];
        }

        private int getLeftChild(int index) {
            return index * 2 + 1;
        }

        private int getRightChild(int index) {
            return index * 2 + 2;
        }

        public int query(int queryL, int queryR) {
            if (queryL < 0 || queryR < 0 || queryL >= data.length || queryR >= data.length || queryL > queryR) {
                throw new IllegalArgumentException("Query failed. Index is illegal.");
            }
            return query(0, 0, data.length - 1, queryL, queryR);
        }

        private int query(int treeIndex, int l, int r, int queryL, int queryR) {
            // 如果完全吻合要查询的区间就可以直接返回了
            if (l == queryL && r == queryR) {
                return tree[treeIndex];
            }
            // 获取中间的索引
            int mid = l + (r - l) / 2;
            int leftChildIndex = getLeftChild(treeIndex);
            int rightChildIndex = getRightChild(treeIndex);

            if (queryR <= mid) { // 查询的区间完全在左半部分
                return query(leftChildIndex, l, mid, queryL, queryR);
            } else if (queryL > mid) { // 查询的区间完全在右半部分
                return query(rightChildIndex, mid + 1, r, queryL, queryR);
            } else { // 查询的部分左右都有
                int leftE = query(leftChildIndex, l, mid, queryL, mid);
                int rightE = query(rightChildIndex, mid + 1, r, mid + 1, queryR);
                return merger.merge(leftE, rightE);
            }
        }
    }

}
